<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="名字" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          clearable
          v-model="queryParams.createTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择创建时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="省份" prop="province">
        <el-input
          v-model="queryParams.province"
          placeholder="请输入省份"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['game:provincial:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['game:provincial:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['game:provincial:remove']"
          >删除</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['game:provincial:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="provincialList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="userId" align="center" prop="userId" />
      <el-table-column label="名字" align="center" prop="name" />
      <el-table-column label="用户名" align="center" prop="username" />
      <el-table-column label="电话" align="center" prop="phone" />
      <el-table-column label="性别" align="center" prop="sex" />
      <el-table-column label="邮箱" align="center" prop="email" />
      <!-- <el-table-column label="${comment}" align="center" prop="roleName" /> -->
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="修改人" align="center" prop="updateBy" />
      <el-table-column label="修改时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="${comment}" align="center" prop="sort" /> -->
      <el-table-column label="用户状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-value="1"
            inactive-value="0"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="省份" align="center" prop="province" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['game:provincial:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['game:provincial:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.rowsPerPage"
      @pagination="getList"
    />

    <!-- 添加或修改省赛管理员对话框 -->
    <el-dialog
      :close-on-click-modal="false"
      :title="title"
      :visible.sync="open"
      width="500px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="名字" prop="name">
          <el-input v-model="form.name" placeholder="请输入名字" />
        </el-form-item>

        <el-form-item
          v-if="form.userId == undefined"
          label="用户名称"
          prop="userName"
        >
          <el-input
            v-model="form.username"
            placeholder="请输入用户名称"
            maxlength="30"
          />
        </el-form-item>

        <el-form-item
          v-if="form.userId == undefined"
          label="用户密码"
          prop="password"
        >
          <el-input
            v-model="form.password"
            placeholder="请输入用户密码"
            type="password"
            maxlength="20"
            show-password
          />
        </el-form-item>
        <el-form-item label="用户性别">
          <el-select v-model="form.sex" placeholder="请选择性别">
            <el-option
              v-for="dict in UserSex"
              :key="dict.id"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in UserStatus"
              :key="dict.id"
              :label="dict.value"
              >{{ dict.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <!-- <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" placeholder="请输入用户名" />
        </el-form-item> -->
        <!-- <el-form-item label="上级ID" prop="parentId">
          <el-input v-model="form.parentId" placeholder="请输入上级ID" />
        </el-form-item> -->
        <!-- <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入密码" />
        </el-form-item> -->
        <el-form-item label="电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入电话" />
        </el-form-item>
        <!-- <el-form-item label="头像" prop="avatar">
          <el-input v-model="form.avatar" placeholder="请输入头像" />
        </el-form-item> -->
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱" />
        </el-form-item>
        <!-- <el-form-item label="${comment}" prop="roleName">
          <el-input v-model="form.roleName" placeholder="请输入${comment}" />
        </el-form-item> -->
        <el-form-item label="省份" prop="province">
          <!-- <el-input v-model="form.province" placeholder="请输入省份" /> -->
          <el-select v-model="form.province" placeholder="请选择省份">
            <el-option
              v-for="item in provinceList"
              :key="item.Id"
              :label="item.name"
              :value="item.name"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listProvincial,
  getProvincial,
  delProvincial,
  addProvincial,
  updateProvincial,
  changeUserStatus,
  delUsers,
} from "@/api/game/provincial";

export default {
  name: "Provincial",
  data() {
    return {
      UserStatus: [
        {
          label: "正常",
          value: "1",
          id: 0,
        },
        {
          label: "停用",
          value: "0",
          id: 1,
        },
      ],
      UserSex: [
        {
          label: "男",
          value: "男",
          id: 0,
        },
        {
          label: "女",
          value: "女",
          id: 1,
        },
      ],
      provinceList: [
        {
          id: 120000,
          name: "天津市",
          parent_id: 0,
        },
        {
          id: 320000,
          name: "江苏省",
          parent_id: 0,
        },
        {
          id: 360000,
          name: "江西省",
          parent_id: 0,
        },
        {
          id: 440000,
          name: "广东省",
          parent_id: 0,
        },
        {
          id: 520000,
          name: "贵州省",
          parent_id: 0,
        },
        {
          id: 640000,
          name: "宁夏回族自治区",
          parent_id: 0,
        },
        {
          id: 130000,
          name: "河北省",
          parent_id: 0,
        },
        {
          id: 210000,
          name: "辽宁省",
          parent_id: 0,
        },
        {
          id: 330000,
          name: "浙江省",
          parent_id: 0,
        },
        {
          id: 370000,
          name: "山东省",
          parent_id: 0,
        },
        {
          id: 410000,
          name: "河南省",
          parent_id: 0,
        },
        {
          id: 450000,
          name: "广西壮族自治区",
          parent_id: 0,
        },
        {
          id: 530000,
          name: "云南省",
          parent_id: 0,
        },
        {
          id: 610000,
          name: "陕西省",
          parent_id: 0,
        },
        {
          id: 650000,
          name: "新疆维吾尔自治区",
          parent_id: 0,
        },
        {
          id: 810000,
          name: "香港特别行政区",
          parent_id: 0,
        },
        {
          id: 140000,
          name: "山西省",
          parent_id: 0,
        },
        {
          id: 220000,
          name: "吉林省",
          parent_id: 0,
        },
        {
          id: 340000,
          name: "安徽省",
          parent_id: 0,
        },
        {
          id: 420000,
          name: "湖北省",
          parent_id: 0,
        },
        {
          id: 460000,
          name: "海南省",
          parent_id: 0,
        },
        {
          id: 500000,
          name: "重庆市",
          parent_id: 0,
        },
        {
          id: 540000,
          name: "西藏自治区",
          parent_id: 0,
        },
        {
          id: 620000,
          name: "甘肃省",
          parent_id: 0,
        },
        {
          id: 820000,
          name: "澳门特别行政区",
          parent_id: 0,
        },
        {
          id: 110000,
          name: "北京市",
          parent_id: 0,
        },
        {
          id: 150000,
          name: "内蒙古自治区",
          parent_id: 0,
        },
        {
          id: 230000,
          name: "黑龙江省",
          parent_id: 0,
        },
        {
          id: 310000,
          name: "上海市",
          parent_id: 0,
        },
        {
          id: 350000,
          name: "福建省",
          parent_id: 0,
        },
        {
          id: 430000,
          name: "湖南省",
          parent_id: 0,
        },
        {
          id: 510000,
          name: "四川省",
          parent_id: 0,
        },
        {
          id: 630000,
          name: "青海省",
          parent_id: 0,
        },
        {
          id: 710000,
          name: "台湾省",
          parent_id: 0,
        },
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 省赛管理员表格数据
      provincialList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        page: 1,
        rowsPerPage: 10,
        name: null,
        // username: null,
        // parentId: null,
        // password: null,
        phone: null,
        // sex: null,
        // avatar: null,
        // email: null,
        // roleName: null,
        // createBy: null,
        createTime: null,
        // updateBy: null,
        // updateTime: null,
        // sort: null,
        // status: null,
        province: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      // 表单校验
      rules: {
        name: [
          { required: true, message: "用户名称不能为空", trigger: "blur" },
          {
            min: 2,
            max: 20,
            message: "用户名称长度必须介于 2 和 20 之间",
            trigger: "blur",
          },
        ],
        username: [
          { required: true, message: "用户昵称不能为空", trigger: "blur" },
        ],
        password: [
          { required: true, message: "用户密码不能为空", trigger: "blur" },
          {
            min: 5,
            max: 20,
            message: "用户密码长度必须介于 5 和 20 之间",
            trigger: "blur",
          },
        ],
        email: [
          {
            type: "email",
            message: "请输入正确的邮箱地址",
            trigger: ["blur", "change"],
          },
        ],
        phone: [
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 用户状态修改
    handleStatusChange(row) {
      let text = row.status === "0" ? "停用" : "启用";
      this.$modal
        .confirm('确认要"' + text + '""' + row.name + '"用户吗？')
        .then(function () {
          return changeUserStatus(row.userId, row.status);
        })
        .then(() => {
          this.$modal.msgSuccess(text + "成功");
        })
        .catch(function () {
          row.status = row.status === "0" ? "0" : "1";
        });
    },
    /** 查询省赛管理员列表 */
    getList() {
      this.loading = true;
      listProvincial(this.queryParams).then((response) => {
        this.provincialList = response.data.list;
        this.total = response.data.totalCount;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        userId: null,
        name: null,
        username: null,
        parentId: null,
        password: null,
        phone: null,
        sex: null,
        avatar: null,
        email: null,
        roleName: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        sort: null,
        status: null,
        province: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.userId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加省赛管理员";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const userId = row.userId || this.ids;
      getProvincial(userId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改省赛管理员";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.userId != null) {
            updateProvincial(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProvincial(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      if (row.userId) {
        this.$modal
          .confirm('是否确认删除省赛管理员编号为"' + row.userId + '"的数据项？')
          .then(function () {
            return delProvincial(userIds);
          })
          .then(() => {
            this.getList();
            this.$modal.msgSuccess("删除成功");
          })
          .catch(() => {});
      }
      if (!row.userId && this.ids) {
        const ids = this.ids;
        const userIds = ids.join(",");
        this.$modal
          .confirm('是否确认删除省赛管理员编号为"' + userIds + '"的数据项？')
          .then(function () {
            return delUsers(userIds);
          })
          .then(() => {
            this.getList();
            this.$modal.msgSuccess("删除成功");
          })
          .catch(() => {});
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "game/provincial/export",
        {
          ...this.queryParams,
        },
        `provincial_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
